package com.zhou;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class MyForm extends JFrame {

	private JLabel urlJLabel = new JLabel("登录网址:");
	private JTextField urlField = new JTextField(
			"http://www.pss-system.gov.cn/sipopublicsearch/portal/uilogin-forwardLogin.shtml");
	private JButton openLoginUrlButton = new JButton("打开网页");

	private JLabel userNameLabel = new JLabel("用户名:");
	private JTextField userNameField = new JTextField("muronghuoxiao3");

	private JLabel passwordLabel = new JLabel("密码:");
	private JPasswordField passwordField = new JPasswordField("19940203XIN");

	private JLabel captchaJLabel = new JLabel("验证码:");
	private JTextField captchaField = new JTextField();
	private JButton loginButton = new JButton("登录");

	private JLabel loginState1Label = new JLabel("登录状态:");
	private JLabel loginState2Label = new JLabel("未登录");
	
	private JCheckBox noVisibleBox = new JCheckBox("不显示浏览器");

	private JLabel searchLabel = new JLabel("检索式:");
	private JTextField searchField = new JTextField("申请日=19850910:20171229");
	private JButton searchButton = new JButton("检索");

	private JLabel searchResultLabel = new JLabel("总共页数:");
	private JLabel sumPageLabel = new JLabel("0");

	private JLabel startPageLabel = new JLabel("起始页:");
	private JTextField startPageField = new JTextField("12536");

	private JLabel endPageLabel = new JLabel("终止页:");
	private JTextField endPageField = new JTextField("20000");

	private JButton downloadButton = new JButton("开始下载");

	private JLabel downloadedLabel = new JLabel("下载进度:");
	private JProgressBar downloadProgressBar = new JProgressBar();
	private JLabel downloadedJLabel = new JLabel("0%");

	private JButton resetButton = new JButton("重来");

	public MyForm() {
		super();

		this.setLayout(null);

		this.add(urlJLabel);
		this.add(urlField);
		this.add(openLoginUrlButton);

		this.add(userNameLabel);
		this.add(userNameField);

		this.add(passwordLabel);
		this.add(passwordField);

		this.add(captchaJLabel);
		this.add(captchaField);
		this.add(loginButton);

		this.add(loginState1Label);
		this.add(loginState2Label);
		this.add(noVisibleBox);		

		this.add(searchLabel);
		this.add(searchField);
		this.add(searchButton);

		this.add(searchResultLabel);
		this.add(sumPageLabel);

		this.add(startPageLabel);
		this.add(startPageField);
		this.add(endPageLabel);
		this.add(endPageField);
		this.add(downloadButton);

		this.add(downloadedLabel);
		this.add(downloadProgressBar);
		this.add(downloadedJLabel);

		this.add(resetButton);

		urlJLabel.setBounds(10, 10, 60, 20);
		urlField.setBounds(80, 10, 200, 20);
		openLoginUrlButton.setBounds(290, 10, 100, 20);

		userNameLabel.setBounds(10, 40, 60, 20);
		userNameField.setBounds(80, 40, 200, 20);

		passwordLabel.setBounds(10, 70, 60, 20);
		passwordField.setBounds(80, 70, 200, 20);

		captchaJLabel.setBounds(10, 100, 60, 20);
		captchaField.setBounds(80, 100, 200, 20);
		loginButton.setBounds(290, 100, 100, 20);

		loginState1Label.setBounds(10, 130, 60, 20);
		loginState2Label.setBounds(80, 130, 200, 20);
		noVisibleBox.setBounds(290, 130, 200, 20);

		searchLabel.setBounds(10, 160, 60, 20);
		searchField.setBounds(80, 160, 200, 20);
		searchButton.setBounds(290, 160, 100, 20);

		searchResultLabel.setBounds(10, 190, 60, 20);
		sumPageLabel.setBounds(80, 190, 60, 20);

		startPageLabel.setBounds(10, 220, 60, 20);
		startPageField.setBounds(80, 220, 200, 20);

		endPageLabel.setBounds(10, 250, 60, 20);
		endPageField.setBounds(80, 250, 200, 20);
		downloadButton.setBounds(290, 250, 100, 20);

		downloadedLabel.setBounds(10, 280, 60, 20);
		downloadProgressBar.setBounds(80, 280, 200, 20);
		downloadedJLabel.setBounds(290, 280, 40, 20);

		resetButton.setBounds(10, 310, 380, 40);

		openLoginUrlButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Spider.login(urlField.getText(), userNameField.getText(),
						String.valueOf(passwordField.getPassword()));
			}
		});
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean b = Spider.inputCaptcha(captchaField.getText());
				if(b){
					loginState2Label.setText("登录成功");
				}else{
					loginState2Label.setText("登录失败");					
				}
			}
		});
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (Spider.driver != null) {
					Spider.driver.quit();
					loginState2Label.setText("未登录");	
				}
			}
		});
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int sumPage = Spider.search(noVisibleBox.isSelected(), searchField.getText());
				sumPageLabel.setText(sumPage+"");
			}
		});
		downloadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Spider.download(Integer.parseInt(startPageField.getText()), Integer.parseInt(endPageField.getText()), "表三");
			}
		});

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (Spider.driver != null) {
					Spider.driver.quit();
				}
			}
		});

		this.setTitle("药物专利检索");
		this.setSize(600, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(300, 20);
		this.setVisible(true);
	}
}
